package com.ysd.lis.service.lab.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.*;
import com.ysd.lis.mapper.pub.PubReportAttributeMapper;
import com.ysd.lis.mapper.pub.PubReportMapper;
import com.ysd.lis.service.lab.PubReportAttributeService;
import com.ysd.util.Base64Util;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.List;

/**
 * <p>
 * cold_chain.PUB_REPORT_ATTRIBUTE 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2023-11-29
 */
@Service
public class PubReportAttributeServiceImpl extends ServiceImpl<PubReportAttributeMapper, PubReportAttribute> implements PubReportAttributeService {
    @Autowired
    PubReportMapper pubReportMapper;
    @Autowired
    PubReportAttributeMapper reportAttributeMapper;

    @Override
    @Transactional
    public Result attributeSave(PubReportAttribute pubReportAttribute)  {
        try {
            if(ToolsUtils.isEmpty(pubReportAttribute)){
                return Result.fail("入参信息不可为空");
            }
            //获取报表中的文件内容，转成xml格式
            if(ToolsUtils.isEmpty(pubReportAttribute.getReportCode())){
                return Result.fail("入参信息【模版】不可为空");
            }
            LambdaQueryWrapper<PubReport> eq = new LambdaQueryWrapper<PubReport>().eq(PubReport::getCode, pubReportAttribute.getReportCode());
            List<PubReport> pubReports = pubReportMapper.selectList(eq);
            PubReport pubReport = null;
            if(CollectionUtil.isNotEmpty(pubReports)){
                pubReport = pubReports.get(0);
            }
            if(ToolsUtils.isEmpty(pubReport)){
                return Result.fail("未获取到模版详情");
            }
            if(ToolsUtils.isEmpty(pubReport.getReportTemplate())){
                return Result.fail("未获取到模版信息");
            }
            String reportTemplate = pubReport.getReportTemplate();
            //将base64的数据转成xml格式数据
            String reportTemplateXML = Base64Util.decodeToString(reportTemplate);
            int index = reportTemplateXML.indexOf("<Report");
            if (index >= 0) {
                reportTemplateXML = reportTemplateXML.substring(index);
            }
            //reportTemplateXML = reportTemplateXML.substring(1);
            //对指定的节点做相应的替换
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(reportTemplateXML)));
            Base64Util.replaceNodeValue(doc,"BottomMargin",pubReportAttribute.getBottomMargin());
            Base64Util.replaceNodeValue(doc,"LeftMargin",pubReportAttribute.getLeftMargin());
            Base64Util.replaceNodeValue(doc,"RightMargin",pubReportAttribute.getRightMargin());
            Base64Util.replaceNodeValue(doc,"TopMargin",pubReportAttribute.getTopMargin());
            Base64Util.replaceNodeValue(doc,"PageHeight",pubReportAttribute.getPaperHigh());
            Base64Util.replaceNodeValue(doc,"PageWidth",pubReportAttribute.getPaperWide());
            String convertToString =  Base64Util.convertToString(doc);
            String base64String = Base64Util.encodeToString(convertToString);
            LambdaUpdateWrapper<PubReport> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(PubReport::getCode,pubReportAttribute.getReportCode()).set(PubReport::getReportTemplate,base64String);
            pubReportMapper.update(null,lambdaUpdateWrapper);
            if(ToolsUtils.isEmpty(pubReportAttribute.getId())){ //对属性做存储
                reportAttributeMapper.insert(pubReportAttribute);
            }else{ //对属性做修改
                reportAttributeMapper.updateById(pubReportAttribute);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ("设置成功");
    }
}